package com.richapp.launcher.log;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.jack.util.date.DateUtil;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Created by jack on 15/12/9.
 */
public class LoggerServer implements Runnable {

    private boolean runFlag = false;
    private int sleepTime = 200;
    private ConcurrentLinkedQueue<String> logList;

    private static LoggerServer loggerServer = null;
    private BufferedOutputStream bos = null;

    private Context context;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    private LoggerServer(){
        logList = new ConcurrentLinkedQueue<String>();
    }

    public static LoggerServer getInstance(){
        if(loggerServer ==  null){
            synchronized (LoggerServer.class){
                if(loggerServer == null){
                    loggerServer = new LoggerServer();
                }
            }
        }
        return  loggerServer;
    }

    @Override
    public void run() {
        String log;
        while(runFlag){
            try {
                log = logList.poll();
                if(log != null){
                    readyOutputStream();
                    if(bos != null){
                        log += "\n";
                        bos.write(log.getBytes());
                        bos.flush();
                    }else{
                        Log.e(getClass().getName(), "输出流空");
                    }
                }

                Thread.sleep(sleepTime);
            }catch (Exception e){
                Log.e(getClass().getName(), "日志线程错误:" + e.getMessage());
                runFlag = false;
            }
        }

        //
        if(bos != null){
            try {
                bos.close();
            } catch (IOException e) {
                Log.e(getClass().getName(), "关闭输出流错误:" + e.getMessage());
            }
        }
    }

    public void addLog(String type,String tag,String content){
        logList.add("time:"+ DateUtil.getMsDate()+"|"+type+"|"+tag+"|"+content);
    }

    private void readyOutputStream(){
        try{
            String fileName = "box."+sdf.format(new Date())+".log";
            String filePath = Environment.getExternalStorageDirectory()+ File.separator+context.getPackageName()+ File.separator+"log"+ File.separator+ fileName;

            File outFile = new File(filePath);
            if(!outFile.getParentFile().exists()){
                outFile.getParentFile().mkdirs();
            }

            if(!outFile.exists()){
                if(bos != null){
                    bos.close();
                    bos = null;
                }
                outFile.setReadable(true);
                outFile.setWritable(true);
                outFile.createNewFile();
            }

            if(bos == null){
                bos = new BufferedOutputStream(new FileOutputStream(outFile));
            }
        }catch (Exception e){
            Log.e(getClass().getName(), "获取文件输入流错误:" + e.getMessage());
        }
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public boolean isRunFlag() {
        return runFlag;
    }

    public void setRunFlag(boolean runFlag) {
        this.runFlag = runFlag;
    }

    public int getSleepTime() {
        return sleepTime;
    }

    public void setSleepTime(int sleepTime) {
        this.sleepTime = sleepTime;
    }
}
